package com.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.gulimall.order.service.OrderService;
import com.gulimall.order.utils.AlipayTemplate;
import com.gulimall.order.vo.PayAsyncVo;
import com.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kkk
 * @date 2022/8/7
 */
@Controller
public class OrderPayController {
    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/alipay", produces = "text/html")
    @ResponseBody
    public String orderPay(@RequestParam String orderSn) throws AlipayApiException {
        PayVo payVo = orderService.getOrderInfo(orderSn);

        return new AlipayTemplate().pay(payVo);
    }

    @PostMapping("/payed/notify")
    @ResponseBody
    public String payedNotify(@RequestParam PayAsyncVo payAsyncVo, HttpServletRequest request) {
        System.out.println(payAsyncVo);

        return "success";
    }
}
